package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.HotGoodsInfo;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.SpannableUtils;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.log.XLog;

public class HomeBaoPinItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<HotGoodsInfo> mBaoPinGoods;
    private OnItemClickListener mOnItemClickListener;

    public HomeBaoPinItemAdapter(Context context, List<HotGoodsInfo> baoPinGoods) {
        mContext = context;
        mBaoPinGoods = baoPinGoods;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (6 == viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_more, parent, false);
            return new ItemViewHolder(view);
        } else{
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_home_goods, parent, false);
            return new ItemGoodsViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemGoodsViewHolder) {
            HotGoodsInfo hotGoodsInfo = mBaoPinGoods.get(position);
            ItemGoodsViewHolder viewHolder = (ItemGoodsViewHolder) holder;
            viewHolder.mGoodsTitleTv.setText(hotGoodsInfo.getProduct_name());
            viewHolder.mGoodsPriceTv.setText("¥" + SpannableUtils.handlePrice(hotGoodsInfo.getSale_price()));
            ImageLoader.getInstance().into(mContext, hotGoodsInfo.getProduct_img(), viewHolder.mGoodsIv);
            viewHolder.mBuyGoodsTv.setOnClickListener(v -> XLog.d(""));

            if (position % 2 == 0) {
                viewHolder.mBg_1.setImageResource(R.drawable.gm_prue_1);
                viewHolder.mBg_2.setImageResource(R.drawable.gm_prue_2);
            } else {
                viewHolder.mBg_1.setImageResource(R.drawable.gm_red_1);
                viewHolder.mBg_2.setImageResource(R.drawable.gm_red_2);
            }
            viewHolder.mBuyGoodsTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onClick(position, false);
                    }
                }
            });
            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onClick(position, false);
                    }
                }
            });
        } else {
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            viewHolder.mLookMoreTv.setOnClickListener(v -> {
                if (mOnItemClickListener != null) mOnItemClickListener.onClick(-1, true);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mBaoPinGoods == null ? 0 : mBaoPinGoods.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mBaoPinGoods.get(position).getViewType();
    }

    public void setNewData(List<HotGoodsInfo> goodsInfos) {
        this.mBaoPinGoods = goodsInfos == null ? new ArrayList<>() : goodsInfos;
//        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(int position, boolean isLookMore);
    }

    private class ItemGoodsViewHolder extends RecyclerView.ViewHolder {

        private ImageView mGoodsIv;
        private TextView mGoodsTitleTv;
        private TextView mGoodsPriceTv;
        private TextView mBuyGoodsTv;
        private ImageView mBg_1;
        private ImageView mBg_2;
        private View mView;

        public ItemGoodsViewHolder(View itemView) {
            super(itemView);
            mView = itemView.findViewById(R.id.item_home_goods_view);
            mGoodsIv = itemView.findViewById(R.id.item_home_goods_pic);
            mGoodsTitleTv = itemView.findViewById(R.id.item_home_goods_title);
            mGoodsPriceTv = itemView.findViewById(R.id.item_home_goods_price);
            mBuyGoodsTv = itemView.findViewById(R.id.item_home_goods_buy);
            mBg_1 = itemView.findViewById(R.id.item_home_goods_bg_iv_1);
            mBg_2 = itemView.findViewById(R.id.item_home_goods_bg_iv_2);
        }
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView mLookMoreTv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            mLookMoreTv = itemView.findViewById(R.id.item_look_more);
        }
    }
}
